<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * 圈圈 Service
 *
 */
class SDOServiceQuanquan extends ServiceBase
{
    # 正式环境Quanquan WSDL URL
    const QQ_WSDL = 'QQ_WSDL';
    # 测试环境Quanquan WSDL URL
    const PRE_QQ_WSDL = 'PRE_QQ_WSDL';
    # 接口调用密码
    const WS_PASSWORD = '098#@!';
    protected static $errMsg = array(
        10001 => "密码校验出错 ", 
        10002 => "输入参数错误 ", 
        10101 => "商户添加出错，商户已存在 ", 
        10102 => "商户修改出错，商户不存在 ", 
        10103 => "获取商户信息出错，商户不存在 ", 
        10201 => "开通IGW失败 ", 
        10202 => "开通IBW失败 ", 
        10301 => "获取IGW Widget列表失败 ", 
        10302 => "获取IBW Widget列表失败 ", 
        10303 => "更新IGW Widget使用列表失败 ", 
        10304 => "更新IBW Widget使用列表失败 "
    );
    protected static $allowPos = array(
        -1=>1, 0=>1, 1=>1, 3=>1, 5=>1
    );
    /// <param name="appId">游戏id</param>
    /// <param name="appName">游戏名称</param>
    /// <param name="principal">负责人</param>
    /// <param name="cellPhone">手机</param>
    /// <param name="telephone">座机</param>
    /// <param name="email">电子邮件</param>
    /// <param name="manager">大客户经理姓名</param>
    /// <param name="developeEngine">开发引擎</param>
    /// <param name="graphicMode">图形模式</param>
    /// <param name="areaServerStruct">区服结构 /param>
    /// <param name="loginMode">登陆模式</param>
    /// <param name="areaGroupMode">游戏选区、组模式</param>
    /// <param name="fullScreenMode">全屏模式</param>
    /// <param name="areaGroupIdValid">初始传入区组ID是否有效</param>
    /// <param name="pigment">色深</param>
    /// <param name="websitePV">网站每日pv</param>
    /// <param name="websiteType">网站类型</param>
    /// <param name="password">核对密码，防止外部非法调用</param>
    /// <returns>成功返回true，失败会抛出异常</returns>
    /**
     * 添加SP信息
     *
     * @param string $message
     * @param array $params  
     * @param string $env
     * @return Boolean
     */
    public static function AddSpInfo (&$message, $params, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $param_keys = array(
            'appId', 
            'appName', 
            'principal', 
            'cellPhone', 
            'telephone', 
            'email', 
            'manager', 
            'developeEngine', 
            'graphicMode', 
            'areaServerStruct', 
            'loginMode', 
            'areaGroupMode', 
            'fullScreenMode', 
            'areaGroupIdValid', 
            'pigment'
        );
        $validate = self::_validateParam($params, $param_keys, $message);
        if ($validate === FALSE) {
            self::setErrNo(SDOServiceErrorCode::API_EC_PARAM);
            return FALSE;
        }
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('AddSpInfo', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $tmp = explode("--->", $message, 2);
            $logLevel = "error";
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json->ErrorCode == 10101) {
                    self::log($errMsg, __CLASS__);
                    return true;
                }
            } else {
                $logLevel = "fatal";
            }
            if ($json && $json->Message) {
                self::setErrNo($json->ErrorCode);
                $logLevel = "warn";
                $message = $json->Message;
            }
            $errMsg[SDK_LOG_REUTRN] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::$logLevel($errMsg, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN]= intval($result);
        self::log($errMsg, __CLASS__, true);
        return TRUE;
    }
    /**
     * 获取SP信息
     *
     * @param string $message
     * @param int $appId
     * @param string $env
     * @return obj|false
     */
    public static function GetSpInfo (&$message, $appId, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('GetSpInfo', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        
        if ($result === FALSE) {       
            $tmp = explode("--->", $message, 2);            
            $logLevel = "error";
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json && $json->Message) {
                    self::setErrNo($json->ErrorCode);
                    $logLevel = $json->ErrorCode!=10103 ? "warn" : 'log';
                    $message = $json->Message;
                }
            } else {
                $logLevel = "fatal";
            }
            $errMsg[SDK_LOG_REUTRN] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::$logLevel($errMsg, __CLASS__);
            return FALSE;
        }
        return $result;
    }
    /**
     * 更新SP信息
     *
     * @param string $message
     * @param array $params  
     * @param string $env
     * @return Boolean
     */
    public static function EditSpInfo (&$message, $params, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $param_keys = array(
            'appId', 
            'appName', 
            'principal', 
            'cellPhone', 
            'telephone', 
            'email', 
            'manager', 
            'developeEngine', 
            'graphicMode', 
            'areaServerStruct', 
            'loginMode', 
            'areaGroupMode', 
            'fullScreenMode', 
            'areaGroupIdValid', 
            'pigment'
        );
        $validate = self::_validateParam($params, $param_keys, $message);
        if ($validate === FALSE) {
            self::setErrNo(SDOServiceErrorCode::API_EC_PARAM);
            return FALSE;
        }
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('EditSpInfo', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $tmp = explode("--->", $message, 2);
            $logLevel = "error";
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json && $json->Message) {
                    self::setErrNo($json->ErrorCode);
                    $logLevel = "warn";
                    $message = $json->Message;
                }
            } else {
                $logLevel = "fatal";
            }
           $errMsg[SDK_LOG_REUTRN] = $message;
           $errMsg[SDK_LOG_LOCATION] = $wsdl;
           self::$logLevel($errMsg, __CLASS__);
           $message = "接口内部错误";
           return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN]= intval($result);
        self::log($errMsg, __CLASS__, true);
        return TRUE;
    }
    /**
     * 开通IGW
     *
     * @param string $message
     * @param int $appId
     * @param string $color 颜色#FFFFFF
     * @param string $skinType 皮肤类型
     * @param string $env
     * @return boolean
     */
    public static function IgwApply (&$message, $appId, $color, $skinType, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        $params['color'] = $color;
        $params['skinType'] = $skinType;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('IgwApply', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $tmp = explode("--->", $message, 2);            
            $logLevel = "error";
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json && $json->Message) {
                    self::setErrNo($json->ErrorCode);
                    $logLevel = "warn";
                    $message = $json->Message;
                }
                
                if (strlen($tmp[1])<60) {
                	$logLevel = "warn";
                }
            } else {
                $logLevel = "fatal";
            }
            $errMsg[SDK_LOG_REUTRN] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::$logLevel($errMsg, __CLASS__); 
            $message = "接口内部错误";
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        $errMsg[SDK_LOG_LOCATION] = $wsdl;
        if ($result->IgwApplyResult) {
            self::log($errMsg, __CLASS__, true);
            return TRUE;
        }
        self::log($errMsg, __CLASS__);
        return FALSE;
    }
    /**
     * 开通 IBW
     *
     * @param string $message
     * @param int $appId
     * @param string $domain
     * @param string $env
     * @return boolean
     */
    public static function IbwApply (&$message, $appId, $domain, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        $params['domain'] = $domain;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('IbwApply', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $tmp = explode("--->", $message, 2);
            $logLevel = "error";
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json && $json->Message) {
                    self::setErrNo($json->ErrorCode);
                    $logLevel = "warn";
                    $message = $json->Message;
                }
            } else {
                $logLevel = "fatal";
            }
            $errMsg[SDK_LOG_REUTRN] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::$logLevel($errMsg, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = intval($result);
        self::log($errMsg, __CLASS__, true);
        return TRUE;
    }
    /**
     * 获取IGW配置信息
     * 第一个数组是皮肤列表
     * 第2个是igw颜色列表
     * 第3个是ibw颜色列表
     * @param string $message
     * @param string $env
     * @return Obj|Boolean
     */
    public static function GetConfig (&$message, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('IGWConfig', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $errMsg[SDK_LOG_REUTRN] = json_encode($message);
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::log($errMsg, __CLASS__);
            return FALSE;
        }
        return $result->IGWConfigResult->ArrayOfRegModel;
    }
    /**
     * 获取Widget配置信息
     *
     * @param string $message
     * @param int $appId
     * @param string $widgetType widget类型，0:igw,1:Ibw
     * @param string $env
     * @return Obj|Boolean $widgetTable IGW时 object(
     * WidgetGUID => Widget的Guid
     * Version => Widget的版本号,
     * WidgetName => Widget的名称,
     * NameSpace => Widget的应用名称,
     * Description => 描述,
     * SPStatus => 当前商户该Widget使用情况 0：不使用,1：启用   
     * )
     * IBW时 object(
     * Idx => 主键ID,
     * WidgetGUID => Widget的Guid
     * Version => Widget的版本号
     * WidgetName => Widget的名称,
     * NameSpace => Widget的应用名称,
     * Description => 描述,
     * SPStatus => 当前商户该Widget使用情况 0：不使用,1：启用 ,
     * Position => Widget所在位置(0 工具集, 1 用户工具, 2 固定栏工具,3 SP专属工具, 
     * 4 侧边栏工具, 5 工具条推荐工具, 6 工具条高亮工具,7 最小化横条工具)
     * )
     */
    public static function GetWidgetList (&$message, $appId, $widgetType, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        $params['type'] = $widgetType;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('GetWidgetList', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $tmp = explode("--->", $message, 2);
            $logLevel = "error";
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json && $json->Message) {
                    self::setErrNo($json->ErrorCode);
                    $logLevel = "warn";
                    $message = $json->Message;
                }
            } else {
                $logLevel = "fatal";
            }
            $errMsg[SDK_LOG_REUTRN] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::$logLevel($errMsg, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        return $result->GetWidgetListResult->AppWidgetStatus;
    }
    /**
     * 提交widget列表的使用信息
     *
     * @param string $message
     * @param int $appId
     * @param array $widgetTable IGW时 array(
     * WidgetGUID => Widget的Guid
     * Version => Widget的版本号,
     * WidgetName => Widget的名称,
     * NameSpace => Widget的应用名称,
     * SPStatus => 当前商户该Widget使用情况 0：不使用,1：禁用   
     * )
     * IBW时 array(
     * Idx => 主键ID,
     * WidgetGUID => Widget的Guid
     * Version => Widget的版本号
     * WidgetName => Widget的名称,
     * NameSpace => Widget的应用名称,
     * SPStatus => 当前商户该Widget使用情况 0：使用,-1：启用 ,
     * Position => Widget所在位置(0 工具集, 1 用户工具, 2 固定栏工具,3 SP专属工具, 
     * 4 侧边栏工具, 5 工具条推荐工具, 6 工具条高亮工具,7 最小化横条工具)
     * )
     * @param int $widgetType widget类型，0:igw,1:Ibw
     * @param string $env
     * @param boolean $auto 当$env为正式环境，且$auto为true时，自动将$widgetTable联调环境内容，转为正式环境的$widgetTable
     * @return Boolean
     */
    public static function UpdateWidgetList (&$message, $appId, $widgetTable, $widgetType, $env = SDK_ENV_TEST, $auto = false)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['appId'] = $appId;
        if ($auto) {
            $fromEnv = SDK_ENV_TEST;
            $toEnv = SDK_ENV_ONLINE;
            if ($env == SDK_ENV_TEST) {
            	$fromEnv = SDK_ENV_ONLINE;
                $toEnv = SDK_ENV_TEST;
            }
            self::parseWidgetTable($widgetTable, $appId, $widgetType, $fromEnv, $toEnv);
        }
        if (! $widgetTable && ! $widgetType) {
            return true;
        }
        $params['widgetTable'] = array_values($widgetTable);
        $disableWDList = self::getDisableWidget($message, $appId, $widgetType, $env);
        $params['widgetTable'] = array_merge($params['widgetTable'], $disableWDList);
        
        $params['type'] = $widgetType;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('UpdateWidgetList', $params, $wsdl, $options, $message);
        $params['auto'] = intval($auto);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $tmp = explode("--->", $message, 2);
            $logLevel = "error";
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json && $json->Message) {
                    self::setErrNo($json->ErrorCode);
                    $logLevel = "warn";
                    $message = $json->Message;
                }
            } else {
                $logLevel = "fatal";
            }
            $errMsg[SDK_LOG_REUTRN] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::$logLevel($errMsg, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        $errMsg[SDK_LOG_LOCATION] = $wsdl;
        if ($result->UpdateWidgetListResult == false) {
        	self::warn($errMsg, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        self::log($errMsg, __CLASS__, true);
        return TRUE;
    }
    /**
     * 联调环境widget列表转义为正式环境的信息
     * @param array $widgetTable
     * @param int $appId
     * @param int $widgetType
     * @param string 原始数据（所在环境）
     * @param string 转化数据（所在环境）
     * 
     * @return void
     */
    private static function parseWidgetTable (&$widgetTable, $appId, $widgetType, $fromEnv=SDK_ENV_TEST, $toEnv=SDK_ENV_ONLINE)
    {
        if (! $widgetTable) {
            return $widgetTable;
        }
        $message = '';
        $wdList = array();
        $preTrans = array();
        $tmp = self::GetWidgetList($message, $appId, $widgetType, $toEnv);
        if (!$tmp) {
        	return $widgetTable;
        }
        if (isset($tmp->NameSpace)) {
        	$tmp = array($tmp);
        }
        foreach ($tmp as $obj) {
            $wdList[$obj->NameSpace] = (array) $obj;
        }
        /*$tmp = self::GetWidgetList($message, $appId, $widgetType, $fromEnv);
        if (isset($tmp->NameSpace)) {
        	$tmp = array($tmp);
        }
        foreach ($tmp as $obj) {
            $preTrans[$obj->WidgetGUID . "-" . $obj->Version] = $obj->NameSpace;
        }*/
        unset($tmp);
        foreach ($widgetTable as $k => $row) {
            $index = $row['NameSpace'];
            if (isset($wdList[$index]) && $tmp = $wdList[$index]) {
                $widgetTable[$k]['Idx'] = $tmp['Idx'];
                $widgetTable[$k]['WidgetGUID'] = $tmp['WidgetGUID'];
                $widgetTable[$k]['Version'] = $tmp['Version'];
            }
        }
    }
    /**
     * 获取快速双，定制联调环境widget列表
     * @param $appId
     * @param $widgetType
     * @return array('options'=>选项, 'selected'=>选中的, 'sp'=>SP专属)|boolean
     */
    public static function getCustomizePreWDList($appId, $widgetType)
    {
        $message = '';
        $onlineWDList = array();
        $other = array();
        $preWDList = array();
        $sp = array();
        $disable = array();
        $tmp = self::GetWidgetList($message, $appId, $widgetType, SDK_ENV_ONLINE);
        if (!$tmp) {
        	return false;
        }
        if (isset($tmp->NameSpace)) {
        	$tmp = array($tmp);
        }
        foreach($tmp as $obj) {
            if (!isset(self::$allowPos[$obj->Position])) {
            	continue;
            }
            $obj->SPStatus = -1;
            if ($obj->Position==1 || $obj->Position==5) {
            	$obj->Position = -1;
            }        
            $obj->env = SDK_ENV_ONLINE;
            $onlineWDList[$obj->NameSpace] = (array)$obj;
        }
        $preWDList = $onlineWDList;
        $tmp = self::GetWidgetList($message, $appId, $widgetType, SDK_ENV_TEST);
        if (!$tmp) {
        	return false;
        }
        if (isset($tmp->NameSpace)) {
        	$tmp = array($tmp);
        }
//        $i = 0;
        foreach($tmp as $obj) {
            if (!isset(self::$allowPos[$obj->Position])) {
            	continue;
            }
//            if (!$i) {
//            	$i++;
//            	continue;
//            }
            $k = $obj->NameSpace;
            if (isset($onlineWDList[$k])) {
            	$tmpObj = $onlineWDList[$k];
                $tmpObj['SPStatus'] = $obj->SPStatus;
                $tmpObj['Position'] = $obj->Position;
                $tmpObj['env'] = SDK_ENV_TEST ;
                if ($obj->Position!=1 && $obj->Position!=5)
                {
                	$preWDList[$k] = $tmpObj;
                	$onlineWDList[$k]['env'] = SDK_ENV_TEST;
                } else {                    
                	$other[] = $tmpObj;
                	if ($onlineWDList[$k]['env'] != SDK_ENV_TEST) {
                		$preWDList[$k]['env'] = SDK_ENV_TEST;
                		$onlineWDList[$k]['env'] = SDK_ENV_TEST;
                	}
                }
            }
        }
        foreach($preWDList as $k=>$obj) {
            if ($obj['Position']==3) {            	
            	$sp[$k] = $obj;
            	unset($preWDList[$k]);
            } elseif ( !$obj['EnableEdit'] ){
            	$disable[$k] = $obj;
            	unset($preWDList[$k]);
            }
            
        }
        unset($onlineWDList);
        unset($tmp);
        return array('options'=>$preWDList, 'selected'=>$other, 'sp'=>$sp, 'disable'=>$disable);
    }
    /**
     * 获取快速单widget列表
     * @param int $appId
     * @param int $widgetType
     * @return array('options'=>选项, 'selected'=>选中的, 'sp'=>SP专属)|boolean
     */
    public static function getDoubleOnlineWDList($appId, $widgetType)
    {
        $message = '';
        $onlineWDList = array();
        $other = array();
        $sp = array();
        $disable = array();
        $tmp = self::GetWidgetList($message, $appId, $widgetType, SDK_ENV_ONLINE);
        if (!$tmp) {
        	return false;
        }
        if (isset($tmp->NameSpace)) {
        	$tmp = array($tmp);
        }
        foreach($tmp as $obj) {
            if (!isset(self::$allowPos[$obj->Position])) {
            	continue;
            }
            $obj->env = SDK_ENV_ONLINE;
            if ($obj->Position!=1 && $obj->Position!=5) {
                if ($obj->Position==3) {
                	$sp[$obj->NameSpace] = (array)$obj;
                	continue;
                }
            	$onlineWDList[$obj->NameSpace] = (array)$obj;
            }            
            else {
                $other[] = (array)$obj;
                if (!isset($onlineWDList[$obj->NameSpace])) {
                    $obj->SPStatus = -1;
                	$onlineWDList[$obj->NameSpace] = (array)$obj;
                }
            }
        }
        foreach($onlineWDList as $k=>$obj) {
        	if(!$obj['EnableEdit']) {
                $disable[$k] = $obj;
               unset($onlineWDList[$k]);
            }
        }
        unset($tmp);
        return array('options'=>$onlineWDList, 'selected'=>$other, 'sp'=>$sp, 'disable'=>$disable);
    }
    
    public static function getDisableWidget(&$message, $appId, $widgetType, $env = SDK_ENV_TEST)
    {
    	$wdList = self::GetWidgetList($message, $appId, $widgetType, $env);
    	$return = array();
    	if ( !$wdList ) {
    		return $return;
    	}
    	if (isset($wdList->NameSpace)) {
        	$wdList = array($wdList);
        }
        foreach($wdList as $obj) {
        	if (isset(self::$allowPos[$obj->Position]) && !$obj->EnableEdit) {
            	$return[] = (array)$obj;
            }
        }
        unset($wdList);
        return $return;
    }
    
    /**
     * 配置IBW皮肤
     *
     * @param string $message
     * @param int $barMode
     * @param string $color
     * @param boolean $useServerConfig
     * @param int $appId
     * @param string $env
     * @return Boolean
     */
    public static function UpdateIBWSkin (&$message, $barMode, $color, $useServerConfig, $appId, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        $params['barMode'] = $barMode;
        $params['color'] = $color;
        $params['useServerConfig'] = $useServerConfig;
        $params['appId'] = $appId;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('UpdateIBWSkin', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $tmp = explode("--->", $message, 2);
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json && $json->Message) {
                    self::setErrNo($json->ErrorCode);
                    $message = $json->Message;
                }
            }
            $errMsg[SDK_LOG_REUTRN] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::log($errMsg, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        self::log($errMsg, __CLASS__, true);
        return TRUE;
    }
    /**
     * 更新商户信息spLogo
     * @param string $message
     * @param int $appId
     * @param string $spLogo
     * @param string $env
     * @return boolean
     */
    public static function IbwRenewSpLogo(&$message, $appId, $spLogo, $env = SDK_ENV_TEST)
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL) : sdk_fetch_config(
        SDK_ENV . self::QQ_WSDL);
        
        $params['spLogo'] = $spLogo;
        $params['appId'] = $appId;
        $params['password'] = self::WS_PASSWORD;
        $options['soap_version'] = SOAP_1_1;
        $result = self::soapRequest('IbwRenewSpLogo', $params, $wsdl, $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params) , SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $tmp = explode("--->", $message, 2);
            if ($tmp[1]) {
                $message = trim($tmp[1]);
                $json = json_decode($message);
                if ($json && $json->Message) {
                    self::setErrNo($json->ErrorCode);
                    $message = $json->Message;
                }
            }
            $errMsg[SDK_LOG_REUTRN] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl;
            self::log($errMsg, __CLASS__);
            $message = "接口内部错误";
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        self::log($errMsg, __CLASS__, true);
        return TRUE;
    }
    /**
     * 自检函数
     */
    public static function selfTest ()
    {
        $arr = array(
            sdk_fetch_config(SDK_ENV . self::QQ_WSDL), 
            sdk_fetch_config(SDK_ENV . self::PRE_QQ_WSDL)
        );
        return self::curlTest($arr);
    }
}
